package com.zheng.flink.study.dataset.counter;

import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.accumulators.LongCounter;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.FileSystem;

import java.util.Arrays;

/**
 * 基于flink的计数器
 * <p>
 * 通过在RuntimeContext注册LongCounter计数器
 * <p>
 * 最后再做汇总
 *
 * @author zhengbo
 * @date 2019/12/18
 */
@Slf4j
public class FlinkBaseCounter {

    public static void main(String[] args) throws Exception {

        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        DataSource<String> dataSource = env.fromCollection(Arrays.asList("zhangsan", "lisi", "zhangyi", "zhengb"));

        DataSet<String> info = dataSource.map(new RichMapFunction<String, String>() {

            LongCounter counter = new LongCounter();

            @Override
            public String map(String value) throws Exception {
                counter.add(1);
                return value;
            }

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                getRuntimeContext().addAccumulator("flink-java-counter", counter);
            }
        });

//        info.writeAsCsv("file:///d:/etc/flink-counter.csv",FileSystem.WriteMode.OVERWRITE).setParallelism(5);
        info.writeAsText("file:///d:/etc/flink-counter", FileSystem.WriteMode.OVERWRITE).setParallelism(5);

        JobExecutionResult jobExecutionResult = env.execute("FlinkBaseCounter");

        long accumulatorResult = jobExecutionResult.getAccumulatorResult("flink-java-counter");

        log.info("counter result:{}", accumulatorResult);

    }
}
